/****************************************************************************
 * Application   : Freekick Desktop Manager
 * Author        : sfreekick
 * Email         : sfreekick@gmail.com
 * Copyright     : (C) 2008 sfreekick
 * **************************************************************************/

/****************************************************************************
 *  This file is part of FreeKickManager.
 *
 *  FreeKickManager is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  FreeKickManager is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with FreeKickManager. If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FreeKickManager
{
    public partial class FrmDiferencias : Form
    {
        public FrmDiferencias()
        {
            InitializeComponent();
        }

        private void colorearEvolucionActPot(ListViewItem li, int colInicial, int numColumnas)
        {
            int valorActual = 0;

            li.UseItemStyleForSubItems = false;

            for (int i = colInicial; i <= colInicial + numColumnas - 1; i++)
            {
                valorActual = Convert.ToInt32(li.SubItems[i].Text);

                li.SubItems[i].ForeColor = Traductor.colorDeHabilidad(valorActual);
            }
        }

        private void colorearEvolucionForma(ListViewItem li, int colInicial, int numColumnas)
        {
            string valorActual = "";

            li.UseItemStyleForSubItems = false;

            for (int i = colInicial; i <= colInicial + numColumnas - 1; i++)
            {
                valorActual = li.SubItems[i].Text;

                li.SubItems[i].ForeColor = Traductor.colorDeFormaAbrev(valorActual);
            }
        }

        public FrmDiferencias(LinkedList<DiferenciaEstado> difs)
        {
            InitializeComponent();

            //Rellenamos las fechas

            if (difs.Count > 0)
            {
                dtpDesde.Value = difs.First.Value.fechaDesde;
                dtpHasta.Value = difs.First.Value.fechaHasta;
            }
            else
            {
                dtpDesde.Value = DateTime.Now;
                dtpHasta.Value = DateTime.Now;
            }

            //Rellenamos las listas con las diferencias

            ListViewItem li = null;

            foreach (DiferenciaEstado d in difs)
            {
                if (d.habilidad.Equals("forma"))
                {
                    li = new ListViewItem(new string[5] { d.idJugador.ToString(), d.apellidos, d.habilidad, Traductor.traducirFormaInglesFormaAbrev(d.antForma), Traductor.traducirFormaInglesFormaAbrev(d.nueForma) });

                    colorearEvolucionForma(li, 3, 2);

                    lstForma.Items.Add(li);
                }
                else if (d.habilidad.Equals("tendencia"))
                {
                    li = new ListViewItem(new string[5] { d.idJugador.ToString(), d.apellidos, d.habilidad, d.antiguo.ToString(), d.nuevo.ToString() });

                    colorearEvolucionForma(li, 3, 2);

                    lstForma.Items.Add(li);
                }
                else
                {
                    if (d.tipo == DiferenciaEstado.ACTUAL)
                    {
                        li = new ListViewItem(new string[6] { d.idJugador.ToString(), d.apellidos, d.habilidad.ToUpper(), d.antiguo.ToString(), d.nuevo.ToString(), d.potencial.ToString() });

                        colorearEvolucionActPot(li, 3, 3);

                        lstActuales.Items.Add(li);
                    }
                    else if (d.tipo == DiferenciaEstado.POTENCIAL)
                    {
                        li = new ListViewItem(new string[5] { d.idJugador.ToString(), d.apellidos, d.habilidad.ToUpper(), d.antiguo.ToString(), d.nuevo.ToString() });

                        colorearEvolucionActPot(li, 3, 2);

                        lstPotenciales.Items.Add(li);
                    }
                }

                //TODO: Completar la lista de "Otras diferencias".
            }
        }

        private void ordenarLista(ListView lst, ColumnClickEventArgs e)
        {
            ColumnSorter cs = new ColumnSorter();
            cs.columnIndex = e.Column;

            if (lst.Columns[e.Column].Tag.Equals("NUM"))
                cs.compareType = ColumnSorter.CompareType.NUMBER;
            else if (lst.Columns[e.Column].Tag.Equals("STR"))
                cs.compareType = ColumnSorter.CompareType.STRING;
            else if (lst.Columns[e.Column].Tag.Equals("DATE"))
                cs.compareType = ColumnSorter.CompareType.DATE;
            else if (lst.Columns[e.Column].Tag.Equals("ACTPOT"))
                cs.compareType = ColumnSorter.CompareType.ACTPOT;

            if (lst.Sorting == SortOrder.Ascending)
                cs.sorting = SortOrder.Descending;
            else
                cs.sorting = SortOrder.Ascending;

            lst.Sorting = cs.sorting;

            lst.ListViewItemSorter = cs;

            //No hace falta:
            //lst.Sort();
        }

        private void lstForma_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            ordenarLista(lstForma, e);
        }

        private void lstActuales_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            ordenarLista(lstActuales, e);
        }

        private void lstPotenciales_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            ordenarLista(lstPotenciales, e);
        }

        private void lstForma_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
        }

        private void lstForma_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            if (lstForma.Items[e.ItemIndex].SubItems[2].Text.Equals("tendencia") && (e.ColumnIndex == 3 || e.ColumnIndex == 4))
            {
                e.DrawDefault = false;

                if ((e.ItemState & ListViewItemStates.Selected) == 0)
                {
                    //Fondo normal
                    e.DrawBackground();
                }
                else
                {
                    //Fondo seleccionado
                    e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
                }

                Bitmap flecha = Traductor.bitmapDeTendencia(Int32.Parse(e.SubItem.Text));

                e.Graphics.DrawImage(flecha, e.Bounds.X + (e.Bounds.Width / 2) - (flecha.Width / 2), e.Bounds.Y + (e.Bounds.Height / 2) - (flecha.Height / 2));
            }
            else
            {
                e.DrawDefault = true;
            }
        }

        private void lstForma_DoubleClick(object sender, EventArgs e)
        {
            FrmJugador frm = new FrmJugador(Int32.Parse(lstForma.SelectedItems[0].Text));
            frm.Show();
        }

        private void lstActuales_DoubleClick(object sender, EventArgs e)
        {
            FrmJugador frm = new FrmJugador(Int32.Parse(lstActuales.SelectedItems[0].Text));
            frm.Show();
        }

        private void lstPotenciales_DoubleClick(object sender, EventArgs e)
        {
            FrmJugador frm = new FrmJugador(Int32.Parse(lstPotenciales.SelectedItems[0].Text));
            frm.Show();
        }
    }
}